<?php
	include_once("database.php");
	include_once 'session_start.php';
	$id = $_GET["id"];
		
    $delgenre = "DELETE FROM itemgenre 
                    WHERE MaterialId=" .$id. ";";
                                	
	$delmaterial = "DELETE FROM item 
					   WHERE MaterialId=".$id.";";	

    $delimage = "DELETE FROM itemimage
						  WHERE MaterialId=".$id.";";
    
    $query = "SELECT * FROM borroweditem WHERE MaterialId=".$id;
    $stmt = $db->query($query);
    if($stmt -> num_rows>0){
        $result = $stmt -> fetch_assoc();
        $_SESSION["H7_Library_Message"]= "Materialet kan ikke slettes da det er udlånt";
        header('Location:loaners.php?show[]='.$result["UserId"]);
        die;
    }

	$db->query($delimage);
    $db ->query($delgenre);
	$db ->query($delmaterial);
	
	header("Location: index.php");
?>